import pygame

player1 = "player"
player2 = "AI"
players = [player1, player2]
current_player = players[0]
turn = 0
first_take_choice = -1
last_move = None
done = False
won = False
winner = None

# 初始化 Pygame 库
pygame.init()

# 显示设置
size = [600, 600]
green = (0, 155, 0)
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode(size)

# 游戏名字
pygame.display.set_caption("Solitaire Game")

# 棋盘的位置数据
POSITION = {
    1: (300, 50),
    2: (417, 118),
    3: (440, 250),
    4: (365, 375),
    5: (235, 403),
    6: (125, 345),
    7: (81, 235),
    8: (105, 118),
    9: (205, 190),
    10: (300, 243)
}

# 定义棋子的类
class Piece:
    def __init__(self, pos_number, color):
        self.color = color  # 棋子颜色
        self.radius = 20   # 棋子半径
        self.x, self.y = POSITION[pos_number]  # 棋子位置
        self.pos_number = pos_number  # 棋子的位置编号，用于翻转颜色

    # 画棋子
    def draw(self):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)

    # 翻转棋子颜色
    def flip_color(self):
        if self.color == black:
            self.color = white
        else:
            self.color = black

    # 移动棋子
    def move(self, dest):
        global last_move

        if last_move is not None:
            # 将 last_move 的颜色翻转
            last_move.flip_color()

        # 移动棋子的方法
        self.x, self.y = POSITION[dest]
        last_move = self

    # 判断是否可以跳到目标棋子
    def can_jump(self, target_piece):
        # 如果有棋子，则不能跳
        if(target_piece is not None):
            return False

        for adj in adjacent_pieces(self.pos_number):
            mid_piece = get_mid_piece(self.pos_number, adj)
            if(mid_piece is None):
                continue
            dest = get_dest(mid_piece.pos_number, self.pos_number, adj)
            if(target_piece == dest):
                return True
        return False

    # 判断棋子是否可以移动到某个位置
    def can_move_to(self, dest):
        # 如果目标已经有棋子，则不能移动
        if get_piece(dest) is not None:
            return False

        for adj in adjacent_pieces(self.pos_number):
            mid_piece = get_mid_piece(self.pos_number, adj)
            # 如果有棋子，则不能跳
            if(mid_piece is None):
                continue
            _dest = get_dest(mid_piece.pos_number, self.pos_number, adj)
            if(dest == _dest):
                return True
        return False

# 得到目标棋子位置
def get_dest(mid, start, end):
    if(mid == start):
        return end
    else:
        return start

# 获取相邻的棋子
def adjacent_pieces(pos_number):
    ADJACENT = {
        1: [2, 9],
        2: [1, 3],
        3: [2, 4],
        4: [3, 5],
        5: [4, 6],
        6: [5, 7],
        7: [6, 8],
        8: [7, 9],
        9: [1, 17]
    }
    return ADJACENT[pos_number]

# 获取中间的棋子
def get_mid_piece(start, end):
    mid = ((start + end) // 2)
    return get_piece(mid)

# 获取某个位置的棋子
def get_piece(pos_number):
    for piece in pieces:
        if piece.pos_number == pos_number:
            return piece
    return None

# 画出棋盘
def draw_board():
    screen.fill(green)

    pygame.draw.polygon(screen, black, [
        (300, 3), (575, 143), (482, 516), (118, 516), (25, 143)
    ], 3)

    for piece in pieces:
        piece.draw()   

    # 添加文本显示
    font = pygame.font.Font(None, 36)
    if done and not won:
        text = font.render(f"游戏结束，你输了，棋子剩余{len(pieces)}颗", True, white)
    elif done and won:
        text = font.render(f"游戏结束，你赢了，棋子剩余{len(pieces)}颗", True, white)
    else:
        text = font.render(f"当前玩家：{current_player}", True, white)
    text_rect = text.get_rect(center=(300, 550))
    screen.blit(text, text_rect)

    pygame.display.flip()

# 判断是否有胜者
def check_for_winner():
    global won, winner
    if len(pieces) == 1:
        won = True
        winner = current_player

# 切换玩家
def switch_player():
    global current_player
    if current_player == players[0]:
        current_player = players[1]
    else:
        current_player = players[0]

# 初始化棋子
pieces = []
for i in range(1, 11):
    pieces.append(Piece(i, black))  # 所有棋子都是黑色的

# 玩家选择第一个被拿走的棋子
while first_take_choice < 1 or first_take_choice > 10:
    try:
        first_take_choice = int(input("请输入拿走的棋子编号 (1-10): "))
    except ValueError:
        print("请输入一个数字 (1-10)！")
        continue

# 根据玩家选择的棋子拿走它
pieces[first_take_choice - 1] = None

# 开始游戏循环
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if not won:
                if current_player == player1:
                    pos = pygame.mouse.get_pos()
                    for piece in pieces:
                        if piece is None:
                            continue

                        # 判断点击位置是否在棋子的半径内
                        distance = ((piece.x - pos[0]) ** 2 +
                                    (piece.y - pos[1]) ** 2) ** 0.5
                        if distance < piece.radius:
                            if piece.can_jump(get_piece(last_move.pos_number)):
                                # 如果可以跳跃，则移动棋子
                                piece.move(last_move.pos_number)
                                check_for_winner()
                                switch_player()

                            elif piece.can_move_to(last_move.pos_number):
                                # 如果可以移动，则移动棋子并删除被跳跃的棋子
                                removed_piece = get_mid_piece(piece.pos_number, last_move.pos_number)
                                pieces.remove(removed_piece)
                                piece.move(last_move.pos_number)

                                # 检查是否有胜者并切换玩家
                                check_for_winner()
                                switch_player()

    # 绘制游戏界面和当前游戏状态
    draw_board()

# 关闭 Pygame 库
pygame.quit()

